package com.sgzn.sgpay.web.controller;


import com.sgzn.sgpay.model.request.SgsMchWithdrawQueryPageRequest;
import com.sgzn.sgpay.model.request.SgsWithdrawRequest;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.MchWithdrawQueryPageResult;
import com.sgzn.sgpay.service.SgsWithdrawService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.MchWithdrawQueryPageRequest;
import com.sgzn.sgpay.web.request.Impl.WithdrawRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 提现控制器
 * @author linliangfang
 */
@Slf4j
@Controller
@RequestMapping("/merchant")
public class WithdrawController extends MerchantBaseController {

    @Resource
    private SgsWithdrawService sgsWithdrawService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    @RequestMapping("withdraw")
    @ResponseBody
    public SgsResultResponse<Map<String, String>> withdraw(WithdrawRequest request){
        super.setMchInfo(request);
        return sgsWithdrawService.withDrawWithWxTools(assembleRequest(request));
    }


    @RequestMapping("withdrawToBank")
    @ResponseBody
    public SgsResultResponse<Map<String, String>> withdrawToBank(WithdrawRequest request){
        super.setMchInfo(request);
        return sgsWithdrawService.withDrawToBank(assembleRequest(request));
    }

    /**
     * 商户提现记录
     * @param request
     * @return
     */
    @RequestMapping("queryWithdrawPage")
    @ResponseBody
    public SgsResultResponse<MchWithdrawQueryPageResult> queryWithdrawPage(MchWithdrawQueryPageRequest request) {
        super.setMchInfo(request);
        return   this.sgsWithdrawService.queryMerchantWithdrawRecordPage(assembleQueryPageRequest(request));

    }

    /**
     * 组装分页查询请求
     * @param request
     * @return
     */
    private SgsMchWithdrawQueryPageRequest assembleQueryPageRequest(MchWithdrawQueryPageRequest request) {
        SgsMchWithdrawQueryPageRequest sgsMchWithdrawQueryPageRequest = new SgsMchWithdrawQueryPageRequest();
        this.assembleCommonHandle.assemble(request, sgsMchWithdrawQueryPageRequest);
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsMchWithdrawQueryPageRequest.setPageQueryParam(pageQueryParam);
        return sgsMchWithdrawQueryPageRequest;
    }

    /**
     * 组装提现请求
     * @param request
     * @return
     */
    private SgsWithdrawRequest assembleRequest(WithdrawRequest request) {
        SgsWithdrawRequest sgsWithdrawRequest = new SgsWithdrawRequest();
        sgsWithdrawRequest.setAmount(request.getAmount());
        assembleCommonHandle.assemble(request, sgsWithdrawRequest);
        return sgsWithdrawRequest;
    }

}

